import { activityApi } from '/api/activity'
import { getServer } from '/store/mutations'
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
//@ts-ignore

Component({
  properties: {
    list: { type: null, value: [] },
    currencyname: String,
  },
  data: {
    wantJoin: false,
    user: wx.getStorageSync(`${getServer()}/logged`).data,
  },

  methods: {
    /** 活动详情 */
    todetail(e: any) {
      const {
        currentTarget: {
          dataset: { id },
        },
      } = e
      wx.navigateTo({
        url: `/pages/activity/detail/detail?id=${id}`,
      })
    },
    /** 加入/取消加入 */
    async join(e: any) {
      const {
        currentTarget: {
          dataset: { item, index },
        },
      } = e

      if (item.user_id === this.data.user.id) {
        return
      }
      if (this.data.wantJoin || item.wantJoin) {
        wx.showToast({
          title: '您已加入过了哟',
          icon: 'none',
          duration: 1000,
        })
        return
      }
      this.setData({
        wantJoin: true,
      })
      try {
        await activityApi.wantJoinActivity(+item.id)
        const list = this.data.list
        list[+index].wantJoin = true
        this.setData({
          list,
          wantJoin: false,
        })
        wx.showToast({
          title: '加入成功',
          icon: 'none',
          duration: 1000,
        })
      } catch (error) {

      }
    },
  },
})
